/**
 * 
 */
package japavo.tools.vocabulary;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 */
public class Vocabulary {

	/**********************************************************************************************
	 ************************************ DECLARATIONS ******************************************** 
	 *********************************************************************************************/
	private @XmlElement String motherTongue;
	private @XmlElement String foreign;
	private @XmlElement String foreignLatin;
	
	private @XmlElement int played;
	private @XmlElement int correct;
	
	
	/**********************************************************************************************
	 ********************************** ENUMS AND CLASSES ***************************************** 
	 *********************************************************************************************/
	public enum ShownWord {MOTHER_TONGUE, FOREIGN, LATIN, NONE;
		
		public ShownWord opposite() {
			switch (this) {
				case MOTHER_TONGUE: return FOREIGN;
				case FOREIGN:
				case LATIN: return MOTHER_TONGUE;
				default: return NONE;
			}
		}
		
	}
	
	/**********************************************************************************************
	 ************************************ CONSTRUCTORS ******************************************** 
	 *********************************************************************************************/
	/**
	 * 
	 */
	public Vocabulary() {
		/* empty */
	}
	
	/**
	 * 
	 */
	public Vocabulary(String german, String foreign, String foreignLatin) {
		this.motherTongue = german;
		this.foreign = foreign;
		this.foreignLatin = foreignLatin;
	}
	
	/**
	 * 
	 */
	public Vocabulary(String german, String foreign) {
		this(german, foreign, foreign);
	}

	/**********************************************************************************************
	 ************************************** FUNCTIONS ******************************************** 
	 *********************************************************************************************/
	public boolean checkCorrectness(String input, ShownWord shownAs) {
		switch (shownAs) {
			case MOTHER_TONGUE: return input.equals(foreign) || input.equals(foreignLatin);
			case FOREIGN:
			case LATIN:
				return input.equals(motherTongue);
		}
		
		return false;
	}
	
	public String getCorrectWord(ShownWord shownAs) {
		switch (shownAs) {
			case MOTHER_TONGUE: return getForeignWithLatin();
			case FOREIGN:
			case LATIN:
				return motherTongue;
		}
		
		return null;
	}
	
	
	/**********************************************************************************************
	 ********************************** GETTER AND SETTER ***************************************** 
	 *********************************************************************************************/
	public String getWord(ShownWord type) {
		switch (type) {
			case MOTHER_TONGUE: return motherTongue;
			case FOREIGN: return foreign;
			case LATIN: return foreignLatin;
			default: return null;
		}
	}
	
	@XmlTransient
	public String getMotherTongue() {
		return this.motherTongue;
	}
	
	public void setMotherTongue(String motherTongue) {
		this.motherTongue = motherTongue;
	}
	
	@XmlTransient
	public String getForeign() {
		return this.foreign;
	}
	
	public void setForeign(String foreign) {
		this.foreign = foreign;
	}
	
	@XmlTransient
	public String getForeignLatin() {
		return this.foreignLatin;
	}
	
	public void setForeignLatin(String foreignLatin) {
		this.foreignLatin = foreignLatin;
	}
	
	public String getForeignWithLatin() {
		return foreign + (foreign.equals(foreignLatin) ? "" : " (" + foreignLatin + ")");
	}
	
	@XmlTransient
	public int getPlayed() {
		return this.played;
	}
	
	@XmlTransient
	public int getCorrect() {
		return this.correct;
	}
	
	public void increasePlayed() {
		this.played++;
	}
	
	public void increaseCorrect() {
		this.correct++;
	}
	
	/**********************************************************************************************
	 ************************************** INHERITED ********************************************* 
	 *********************************************************************************************/
	@Override
	public String toString() {
		return motherTongue
				+ " = "
				+ getForeignWithLatin()
				+ " | " + correct + "/" + played;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Vocabulary))
			return false;
		Vocabulary that = (Vocabulary) obj;
		return that.motherTongue.equals(this.motherTongue) && that.foreign.equals(this.foreign);
	}
}
